package com.jeff.tianti.controller.channel;

import com.jeff.tianti.cms.entity.Organization;
import com.jeff.tianti.cms.service.OrganizationService;
import javax.servlet.http.HttpServletRequest;

import com.jeff.tianti.common.dto.AjaxResult;
import com.jeff.tianti.util.Constants;
import org.apache.commons.lang3.StringUtils;
import java.util.Date; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Controller; 
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import com.jeff.tianti.common.entity.PageModel; 
import com.jeff.tianti.cms.dto.OrganizationQueryDTO;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author NOW_GO
 * @desc OrganizationController 
 * @date 2019-07-12
 */
@Controller
@RequestMapping("/channel/organization")
public class OrganizationController{

    @Autowired
    private OrganizationService organizationService;

    @RequestMapping("/list")
    public String list(HttpServletRequest request,Model model){
       String currentPageStr = request.getParameter("currentPage");
       String pageSizeStr = request.getParameter("pageSize");
       String organizeName = request.getParameter("organizeName");
       int currentPage = 1;
       int pageSize = 10;
       if(StringUtils.isNotBlank(currentPageStr)){
             currentPage = Integer.parseInt(currentPageStr);
       }
       if(StringUtils.isNotBlank(pageSizeStr)){
             pageSize = Integer.parseInt(pageSizeStr);
       }
       OrganizationQueryDTO organizationQueryDTO = new OrganizationQueryDTO(); 
       organizationQueryDTO.setCurrentPage(currentPage);
       organizationQueryDTO.setPageSize(pageSize);
       organizationQueryDTO.setOrganizeName(organizeName);
       PageModel<Organization> page = this.organizationService.queryOrganizationPage(organizationQueryDTO);
       model.addAttribute("page", page);
       model.addAttribute("organizationQueryDTO", organizationQueryDTO);
        model.addAttribute(Constants.MENU_NAME, Constants.MESSAGE_ORGANIZATION);
       return "/channel/organization/list";
    }

    @RequestMapping("/add")
    public String add(HttpServletRequest request,Model model){
        String id = request.getParameter("id");
        Organization organization = null;
        if(StringUtils.isNotBlank(id)){
             organization = this.organizationService.find(id);
        }
        model.addAttribute("organization", organization);
        model.addAttribute("id", id);
        return "/channel/organization/edit";
    }

    @RequestMapping("/saveorupdate")
    @ResponseBody
    public AjaxResult doSaveOrUpdate(HttpServletRequest request){
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(false);

        try {
            String id = request.getParameter("id");
            String organizeName = request.getParameter("organizeName");
            String organizeDetail = request.getParameter("organizeDetail");
            Organization organization = null;
            if(StringUtils.isNotBlank(id)){
                 organization = this.organizationService.find(id);
                 organization.setUpdateDate(new Date());
                 organization.setOrganizeDetail(organizeDetail);
                 organization.setOrganizeName(organizeName);
                 this.organizationService.update(organization);
            }else{
                 organization = new Organization();
                 organization.setCreateDate(new Date());
                organization.setOrganizeDetail(organizeDetail);
                organization.setOrganizeName(organizeName);
                 this.organizationService.save(organization);
            }
            ajaxResult.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ajaxResult;
    }

    @RequestMapping("/doDelete")
    @ResponseBody
    public AjaxResult doDelete(HttpServletRequest request){
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(false);

        try {
            String id = request.getParameter("id");
            Organization organization = this.organizationService.find(id);
            if(organization != null){
               organization.setDeleteFlag("1");
               this.organizationService.update(organization);
            }
            ajaxResult.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ajaxResult;
    }



}